Imports System.IO
Imports System.IO.Compression

Namespace Core.Net.Compression
    Public MustInherit Class Compressor
        Public Shared Function compress(ByVal pBA_Uncompressed As Byte()) As Byte()
            Dim msOut As New MemoryStream()

            Dim ds As New DeflateStream(msOut, CompressionMode.Compress)
            ds.Write(pBA_Uncompressed, 0, pBA_Uncompressed.Length)
            ds.Close()

            Return msOut.ToArray
        End Function

        Public Shared Function decompress(ByVal pBA_Compressed As Byte()) As Byte()
            Const BufferSize As Integer = 1024

            Dim msIn As New MemoryStream(pBA_Compressed)
            Dim msOut As New MemoryStream

            Dim ds As New DeflateStream(msIn, CompressionMode.Decompress)

            Dim baBuffer As Byte() = New Byte(BufferSize - 1) {}
            Dim iRead As Integer

            While True
                iRead = ds.Read(baBuffer, 0, BufferSize)

                If iRead = 0 Then
                    Exit While
                End If

                msOut.Write(baBuffer, 0, iRead)
            End While

            ds.Close()
            msOut.Close()

            Return msOut.ToArray
        End Function
    End Class
End Namespace
